<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\services\impl;


use Selibra\Di\Annotations\Autowired;
use Selibra\Di\DI;
use Selibra\Model\EntityProxy;
use Selibra\Tools\Console;
use Services\System\Cache\RegionCache;
use ckeeper\dao\RegionDao;
use ckeeper\entity\RegionEntity;
use ckeeper\services\RegionService;

/**
 * 地区服务类
 * Class RegionServiceImpl
 * @package ckeeper\services\impl
 */
class RegionServiceImpl implements RegionService
{
    /**
     * 地区dao
     * @var RegionDao
     */
    #[Autowired]
    protected RegionDao $regionDao;

    /**
     * 获取城市（二级）列表
     * @param int $pid
     * @return array
     */
    public function getCityList(int $pid): array
    {
        /** @var RegionCache $regionCache */
        $regionCache = DI::getObjectContext(RegionCache::class);
        $data = $regionCache->getRegionByKey($pid);
        if ($data === null) {
            $regionData = $this->regionDao->getRegionByPid($pid);
            if (empty($regionData)) {
                $regionCache->writeRegionCache($pid, null);
                return [];
            }
            $regionCache->writeRegionCache($pid, $regionData);
            $data = $regionData;
        } elseif ($data === false) {
            return [];
        }
        return $data;
    }

    /**
     * 获取所有地址
     * @return array
     */
    public function getAllRegionList(): array
    {
        /** @var RegionCache $regionCache */
        $regionCache = DI::getObjectContext(RegionCache::class);
        $data = $regionCache->getRegionByKey('all');
        if ($data === null) {
            $allRegionData = $this->regionDao->getAllRegion();
            if (empty($allRegionData)) {
                $regionCache->writeRegionCache('all', null);
                return [];
            }
            $regionCache->writeRegionCache('all', $allRegionData);
            $data = $allRegionData;
        } elseif ($data === false) {
            return [];
        }
        return $data;
    }

    /**
     * 根据id获取地址详情
     * @param int $id
     * @return array
     */
    public function getRegionDetailById(int $id): array
    {
        /** @var RegionCache $regionCache */
        $regionCache = DI::getObjectContext(RegionCache::class);
        $data = $regionCache->getRegionByKey($id);
        if ($data === null) {
            $regionEntity = $this->regionDao->getRegionById($id);
            if (empty($regionEntity)) {
                $regionCache->writeRegionCache($id, null);
                return [];
            }
            $regionData = EntityProxy::entityConversionToArray($regionEntity);
            $regionCache->writeRegionCache($id, $regionData);
            $data = $regionData;
        } elseif ($data === false) {
            return [];
        }
        return $data;
    }

    /**
     * 获取地区名称
     * @param int $id
     * @return string
     */
    public function getRegionName(int $id): string
    {
        $region = $this->getRegionDetailById($id);
        return $region['name'] ?: '';
    }

    /**
     * @param string $name
     * @param int $level
     * @param string|null $cityName
     * @param string|null $provinceName
     * @return array|null
     */
    public function getRegionByNameAndLevel(string $name, int $level, ?string $cityName = null, ?string $provinceName = null): ?array
    {
        /** @var RegionCache $regionCache */
        $regionCache = DI::getObjectContext(RegionCache::class);
        $cacheKey = $name . $cityName . $provinceName;
        $cache = $regionCache->getRegionByMultipleName($cacheKey);
        if ($cache === false) {
//            return null;
        }
        if (true || empty($cache)) {
            $parentRegion = null;
            if (!empty($provinceName)) {
                $parentRegion = $this->regionDao->getRegionByNameAndLevel($provinceName, 1);
                if (empty($parentRegion)) {
                    return null;
                }
            }
            if ($level == 3 && !empty($cityName)) {
                $parentRegion = $this->getRegionByNameAndLevel($cityName, 2, null, $parentRegion ? $parentRegion->getName() : null);
                if (empty($parentRegion)) {
                    return null;
                }
            }
            $data = $this->regionDao->getRegionByNameAndLevel($name, $level);
            if (empty($data)) {
                $regionCache->writeRegionByMultipleNameCache($cacheKey, null);
                return null;
            }
            $data = EntityProxy::entityConversionToArray($data);
            $regionCache->writeRegionByMultipleNameCache($cacheKey, $data);
        } else {
            $data = $cache;
        }
        return $data;
    }
}
